<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Service\ApiService;  //接口服务调用api
class OnlineCourseController extends CommonController {
	/**
	 * 课程列表 
	 */
    public function index() {
    	
    	$Model = M("OnlineCourse");
    	$total = $Model->where($map)->count();
    	
    	$page = new \Think\Page($total, 10, $REQUEST);
    	// if($total>$listRows){
    	$page->setConfig('theme', ' %UP_PAGE% %LINK_PAGE% %DOWN_PAGE%  %HEADER%');
    	// }
    	$p = $page->show();
    	$this->assign('_page', $p ? $p : '');
    	$this->assign('_total', $total);
    	$order = "create_time desc";
    	$list = $Model->where($map)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
    	$this->assign("_list", $list);
    	
    	
        $this->meta_title="课程列表";
        $this->display();
    }
    /**
     * 课程添加
     */
    public function add(){
    	if (IS_POST){
    		try{
    			$client = new ApiService;
    			$clientapi = $client->useService("OnlineCourse");
    			$res=$clientapi->addData($_POST);
    		
    			$return_info=json_decode($res,true);
    		
    			if(!empty($return_info["code"])){	
    				$this->success($return_info["msg"],U($return_info["url"]));
    			}else{
    				$this->error($return_info["msg"]);
    			}
    		}catch (\Exception $e){
    		  $this->error($e->getMessage());
    		}
    	}
    	$this->meta_title="课程添加";
    	$this->display();
    }
    /**
     * 课程编辑
     */
    public function edit(){
    	if (IS_POST){
    		try{
    			$client = new ApiService;
    			$clientapi = $client->useService("OnlineCourse");
    			$res=$clientapi->saveData($_POST);
    			
    			$return_info=json_decode($res,true);
    			if($return_info["code"]){
    				
    				$this->success($return_info["msg"],U($return_info["url"]));
    			}else{
    				$this->error($return_info["msg"]);
    			}
    		}catch (\Exception $e){
    			$this->error($e->getMessage());
    		}
    	}
    	$map["id"]=I("id");
    	$Model=M("OnlineCourse");
    	$info=$Model->where($map)->find();
    	$this->assign("info",$info);
    	$this->meta_title="课程编辑";
    	$this->display();
    }
    /**
     * 课程删除
     */
    public function del($id){
    	$client = new ApiService;
    	$clientapi = $client->useService("OnlineCourse");
    	$res=$clientapi->delData($id);
    	$return_info=json_decode($res,true);
    	if($return_info["code"]){
    		$this->success($return_info["msg"],$return_info["url"]);
    	}else{
    		$this->error($return_info["msg"]);
    	}
    }
   
   
}
